package com.org.api.admin.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.type.TypeReference;
import com.org.api.admin.controller.CmsUserRedisController;
import com.org.api.admin.entity.po.*;
import com.org.api.admin.service.CmsUserMobileNavigationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Description
 * @auther dh
 * @date 2022/3/3 10:45
 */
@Slf4j
@Service
public class CmsUserMobileNavigationServiceImpl implements CmsUserMobileNavigationService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<CmsUserMobileNavigationDetailVO> getModuleDetailData(CmsUserRedisIndexModule cmsUserRedisIndexModule) {
        // 这里通过模块id查询对应未删除的导航数据
        List<CmsUserMobileNavigationDetailVO> dataList = new ArrayList<>();
        CmsUserMobileNavigationDetailVO cmsUserMobileNavigationDetailVO = new CmsUserMobileNavigationDetailVO();
        cmsUserMobileNavigationDetailVO.setConfigId(1L);
        cmsUserMobileNavigationDetailVO.setTitle("测试导航");
        cmsUserMobileNavigationDetailVO.setNavType("1");
        cmsUserMobileNavigationDetailVO.setOrderSort(1);
        dataList.add(cmsUserMobileNavigationDetailVO);
        cmsUserMobileNavigationDetailVO = new CmsUserMobileNavigationDetailVO();
        cmsUserMobileNavigationDetailVO.setConfigId(2L);
        cmsUserMobileNavigationDetailVO.setTitle("测试导航2");
        cmsUserMobileNavigationDetailVO.setNavType("2");
        cmsUserMobileNavigationDetailVO.setOrderSort(2);
        dataList.add(cmsUserMobileNavigationDetailVO);
        return dataList;
    }

    @Override
    public void invokeModuleDetailData(CmsUserRedisIndexModule cmsUserRedisIndexModule) {
        String moduleDetailKey = getDetailKey(CmsUserModuleEnum.Navigation.getModuleType(), cmsUserRedisIndexModule.getModuleConfigId());
        Integer isDelete = cmsUserRedisIndexModule.getIsDelete();
        if (null != isDelete && isDelete.equals(1)) {
            redisTemplate.delete(moduleDetailKey);
            return;
        }
        redisTemplate.delete(moduleDetailKey);
        List<CmsUserMobileNavigationDetailVO> cmsUserMobileNavigationDetailVOList = null;
        try {
            Object object = cmsUserRedisIndexModule.getModuleDetailData();
            cmsUserMobileNavigationDetailVOList = CmsUserRedisServiceImpl.getObjectMapper().readValue(JSONObject.toJSONString(object), new TypeReference<List<CmsUserMobileNavigationDetailVO>>() {
            });
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
        CmsUserMobileNavigationVO cmsUserMobileNavigationVO = new CmsUserMobileNavigationVO();
        cmsUserMobileNavigationVO.setIsLongTerm(CmsUserRedisController.isLongTeam);
        cmsUserMobileNavigationVO.setShowStartTime(CmsUserRedisController.startTimeString);
        cmsUserMobileNavigationVO.setShowEndTime(CmsUserRedisController.endTimeString);
        cmsUserMobileNavigationVO.setList(cmsUserMobileNavigationDetailVOList);
        redisTemplate.opsForValue().set(moduleDetailKey, cmsUserMobileNavigationVO);
        long expireTime = cmsUserRedisIndexModule.getExpireTime();
        if (expireTime > 0) {
            redisTemplate.expire(moduleDetailKey, expireTime, TimeUnit.MILLISECONDS);
        }
    }

    @Override
    public CmsUserMobileNavigationVO getRedisDataList(CmsUserRedisIndexModule cmsUserRedisIndexModule) {
        String moduleDetailKey = getDetailKey(CmsUserModuleEnum.Navigation.getModuleType(), cmsUserRedisIndexModule.getModuleConfigId());
        Object object = redisTemplate.opsForValue().get(moduleDetailKey);
        CmsUserMobileNavigationVO cmsUserMobileNavigationVO = CmsUserRedisServiceImpl.getObjectMapper().convertValue(object, CmsUserMobileNavigationVO.class);
        cmsUserMobileNavigationVO.setModuleType(CmsUserModuleEnum.Navigation.getModuleType());
        cmsUserMobileNavigationVO.setOrderSort(cmsUserRedisIndexModule.getOrderSort());
        return cmsUserMobileNavigationVO;
    }
}
